import {
  CREATE_OPEN_SHOP_ORDER,
  CREATE_OPEN_SHOP_PAY_ORDER,
  wechatGo
} from '@/api/new-open-shop'

export default {
  computed: {},
  methods: {
    openShopPay (identity, amount) {
      const vm = this

      if (amount === '0' || amount === '0.0' || amount === '免费') {
        this.$router.push(
          `/pages/new-open-shop/id-card/main?identity=${identity}`
        )
        return false
      }

      let customerId = vm.$cookie.get('store-login').customerId || vm.$cookie.get('upgrade-shop-customerId')
      let providerId = vm.$store.state.account.providerId

      let params = {
        customerId,
        providerId,
        shopIdentity: identity,
        openShopType: vm.$cookie.get('IS-UPGRADE') ? 'UPGRADE' : 'OPEN'
      }

      CREATE_OPEN_SHOP_ORDER(params)
        .then(r => {
          if (r.data.code === '0') {
            const orderNo = r.data.data

            vm.$cookie.set('openShopOrderNo', orderNo)
            if (amount === '0' || amount === '0.0' || amount === '免费') {
              vm._createPayZero(orderNo, identity)
            } else {
              vm._createPayOrder(orderNo, identity)
              // let linkAmount = ''
              // if (amount.indexOf(' ') !== -1) {
              //   linkAmount = amount.split(' ')[1]
              // } else {
              //   linkAmount = amount
              // }
              // this.$router.push(
              //   `/pages/appPays/main?paySourceEnum=H5&type=OPENSHOP&appId=${this.$store.state.shopAppId}&openShopOrderNo=${orderNo}&amount=${linkAmount}`
              // )
            }
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    },

    _createPayOrder (orderNo, identity) {
      const vm = this

      let params = {
        openShopOrderNo: orderNo,
        payMethodEnum: 'MINI',
        paySourceEnum: 'H5',
        phone: vm.$cookie.get('store-login').mobile,
        openId: vm.$cookie.get('openId'),
        payMerId: vm.$store.state.payMerId
      }

      CREATE_OPEN_SHOP_PAY_ORDER(params)
        .then(r => {
          if (r.data.code === '0') {
            wechatGo(r.data.data, vm, 'thisInOpenShop', () => {
              this.$router.push(
                `/pages/new-open-shop/id-card/main?identity=${identity}`
              )
            })
          } else {
            console.log(123)
            vm.$toast(r.data.message)
          }
          vm.btnDis = false
        })
        .catch(e => {
          vm.btnDis = false
          console.error(e)
        })
    },
    _createPayZero (orderNo, identity) {
      const vm = this

      let params = {
        openShopOrderNo: orderNo,
        payMethodEnum: 'MINI',
        paySourceEnum: 'H5',
        phone: vm.$cookie.get('store-login').mobile,
        openId: vm.$cookie.get('openId'),
        payMerId: vm.$store.state.payMerId
      }
      CREATE_OPEN_SHOP_PAY_ORDER(params)
        .then(r => {
          if (r.data.code === '0') {
            this.$router.push(
              `/pages/new-open-shop/id-card/main?identity=${identity}`
            )
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    }
  }
}
